<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <title>首页 - 教师测评系统</title>
  <!-- Bootstrap 5 CSS -->
  <link th:href="@{/static/css/bootstrap.min.css}" rel="stylesheet">
  <!-- Bootstrap Icons -->
  <link th:href="@{/static/css/bootstrap-icons.css}" rel="stylesheet">

  <style>
    :root {
      --primary-color: #3f6ad8;
      --primary-light: rgba(63, 106, 216, 0.1);
      --primary-dark: #2952bb;
      --primary-gradient: linear-gradient(135deg, #3f6ad8 0%, #2952bb 100%);
      --secondary-color: #6c757d;
      --success-color: #2dce89;
      --success-light: rgba(45, 206, 137, 0.1);
      --warning-color: #fb6340;
      --warning-light: rgba(251, 99, 64, 0.1);
      --danger-color: #f5365c;
      --light-color: #f8f9fe;
      --dark-color: #344767;
      --text-muted: #8a94a6;
      --border-radius: 16px;
      --card-shadow: 0 7px 25px rgba(0, 0, 0, 0.08);
      --hover-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
    }

    body {
      font-family: 'Noto Sans SC', 'Poppins', sans-serif;
      background-color: var(--light-color);
      background-image:
        radial-gradient(circle at 5% 10%, rgba(63, 106, 216, 0.05) 0%, transparent 15%),
        radial-gradient(circle at 95% 90%, rgba(63, 106, 216, 0.05) 0%, transparent 15%);
      padding-bottom: 80px; /* 为底部导航栏预留空间 */
      color: var(--dark-color);
      min-height: 100vh;
    }

    /* 导航栏 */
    .nav-bottom {
      position: fixed;
      bottom: 0;
      width: 100%;
      height: 66px;
      background-color: #fff;
      box-shadow: 0 -5px 25px rgba(0, 0, 0, 0.07);
      display: flex;
      z-index: 1000;
      border-radius: 24px 24px 0 0;
      padding: 0 10px;
    }

    .nav-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      color: var(--text-muted);
      text-decoration: none;
      font-size: 12px;
      line-height: 1;
      transition: all 0.3s ease;
      position: relative;
      font-weight: 500;
      padding: 12px 0;
    }

    .nav-item.active {
      color: var(--primary-color);
    }

    .nav-item.active:after {
      content: '';
      position: absolute;
      bottom: 8px;
      width: 24px;
      height: 3px;
      background-color: var(--primary-color);
      border-radius: 3px;
    }

    .nav-icon {
      font-size: 24px;
      margin-bottom: 5px;
    }

    /* 页面容器 */
    .page-container {
      padding-top: 25px;
      padding-bottom: 30px;
      max-width: 540px;
      margin: 0 auto;
    }

    /* 卡片样式 */
    .card {
      border-radius: var(--border-radius);
      box-shadow: var(--card-shadow);
      margin-bottom: 24px;
      border: none;
      transition: transform 0.3s ease, box-shadow 0.3s ease;
      overflow: hidden;
      background-color: #fff;
    }

    .card:hover {
      transform: translateY(-5px);
      box-shadow: var(--hover-shadow);
    }

    .card-body {
      padding: 24px;
    }

    /* 当前周期卡片 */
    .period-card {
      background: #fff;
      border-left: 4px solid var(--primary-color);
      position: relative;
      overflow: hidden;
    }

    .period-card::before {
      content: '';
      position: absolute;
      top: -10px;
      right: -10px;
      width: 100px;
      height: 100px;
      background: var(--primary-light);
      border-radius: 50%;
      opacity: 0.7;
      z-index: 0;
    }

    /* 标题样式 */
    .section-title {
      font-size: 18px;
      font-weight: 600;
      margin-bottom: 24px;
      color: var(--dark-color);
      display: flex;
      align-items: center;
      padding-left: 16px;
      position: relative;
    }

    .section-title:before {
      content: '';
      position: absolute;
      left: 0;
      width: 4px;
      height: 20px;
      background-color: var(--primary-color);
      border-radius: 2px;
    }

    /* 任务卡片 */
    .task-card {
      position: relative;
      overflow: hidden;
      border: 1px solid rgba(230, 235, 250, 0.7);
    }

    .task-card .badge {
      font-weight: 500;
      padding: 7px 12px;
      border-radius: 30px;
      font-size: 12px;
    }

    .task-badge-completed {
      background-color: var(--success-light) !important;
      color: var(--success-color) !important;
    }

    .task-badge-pending {
      background-color: var(--warning-light) !important;
      color: var(--warning-color) !important;
    }

    .task-info {
      display: flex;
      flex-direction: column;
      gap: 10px;
    }

    .task-meta {
      display: flex;
      align-items: center;
      font-size: 13px;
      color: var(--text-muted);
    }

    .task-meta i {
      margin-right: 8px;
      font-size: 15px;
      color: var(--text-muted);
    }

    .btn-action {
      border-radius: 12px;
      padding: 10px 18px;
      font-weight: 500;
      box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
      transition: all 0.3s;
      font-size: 14px;
    }

    .btn-action:hover {
      transform: translateY(-3px);
      box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
    }

    .btn-action:active {
      transform: translateY(-1px);
    }

    .btn-primary {
      background: var(--primary-gradient);
      border: none;
    }

    .btn-outline-primary {
      border-color: var(--primary-color);
      color: var(--primary-color);
    }

    /* 加载动画 */
    .loading-container {
      padding: 60px 0;
      text-align: center;
    }

    .spinner-border {
      width: 3rem;
      height: 3rem;
      color: var(--primary-color);
    }

    /* 空状态样式 */
    .empty-state {
      text-align: center;
      padding: 50px 0;
    }

    .empty-state-icon {
      font-size: 48px;
      color: #d1d9e6;
      margin-bottom: 20px;
    }

    /* 系统通知横幅 */
    .system-banner {
      background: linear-gradient(120deg, #f0f7ff, #eaf2ff);
      border-radius: var(--border-radius);
      padding: 18px 22px;
      margin-bottom: 24px;
      display: flex;
      align-items: center;
      border-left: 4px solid var(--primary-color);
      position: relative;
      overflow: hidden;
    }

    .system-banner::after {
      content: '';
      position: absolute;
      right: -20px;
      top: -20px;
      width: 80px;
      height: 80px;
      background-color: rgba(63, 106, 216, 0.1);
      border-radius: 50%;
    }

    .system-banner-icon {
      font-size: 26px;
      color: var(--primary-color);
      margin-right: 18px;
      position: relative;
      z-index: 1;
    }

    /* 新增的圆形图标容器样式 */
    .icon-circle {
      width: 46px;
      height: 46px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 16px;
      flex-shrink: 0;
    }

    .icon-circle-primary {
      background-color: var(--primary-light);
      color: var(--primary-color);
    }

    /* 引入动画效果 */
    @keyframes fadeInUp {
      from {
        opacity: 0;
        transform: translateY(20px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    .fadeInUp {
      animation: fadeInUp 0.6s ease forwards;
    }
  </style>
</head>
<body>
<!-- 页面内容 -->
<div class="container page-container">
  <!-- 系统通知横幅 -->
  <div class="system-banner fadeInUp" style="animation-delay: 0.1s">
    <div class="system-banner-icon">
      <i class="bi bi-megaphone-fill"></i>
    </div>
    <div>
      <div class="fw-medium">欢迎使用教师测评系统！</div>
      <small class="text-muted">请按时完成测评任务，确保评价公正客观</small>
    </div>
  </div>

  <!-- 当前周期信息 -->
  <div class="card period-card fadeInUp" style="animation-delay: 0.2s">
    <div class="card-body">
      <div th:if="${currentPeriod != null}">
        <div class="d-flex align-items-center mb-4">
          <div class="icon-circle icon-circle-primary">
            <i class="bi bi-calendar-check" style="font-size: 22px;"></i>
          </div>
          <h5 class="card-title mb-0 fw-bold" th:text="${currentPeriod.name}">当前评测周期名称</h5>
        </div>
        <div class="d-flex flex-column ps-2">
          <div class="text-muted small mb-3 d-flex align-items-center">
            <i class="bi bi-clock me-2"></i>
            <span>开始时间: </span>
            <span class="ms-2 text-dark fw-medium"
                  th:text="${currentPeriod != null ? currentPeriod.startTimeStr : '未设置'}">2023-01-01</span>
          </div>
          <div class="text-muted small d-flex align-items-center">
            <i class="bi bi-flag me-2"></i>
            <span>结束时间: </span>
            <span class="ms-2 text-dark fw-medium"
                  th:text="${currentPeriod != null ? currentPeriod.endTimeStr : '未设置'}">2023-01-31</span>
          </div>
        </div>
      </div>
      <div th:if="${currentPeriod == null}" class="empty-state">
        <i class="bi bi-calendar-x empty-state-icon"></i>
        <p class="text-muted mb-0">当前没有正在进行的评测周期</p>
      </div>
    </div>
  </div>

  <!-- 测评任务列表 -->
  <h6 class="section-title fadeInUp" style="animation-delay: 0.3s">
    <i class="bi bi-list-check me-2"></i> 我的测评任务
  </h6>

  <div id="taskList" class="fadeInUp" style="animation-delay: 0.4s">
    <!-- 骨架屏预加载效果 -->
    <div class="skeleton-loader">
      <div class="card task-card mb-3">
        <div class="card-body">
          <div class="d-flex justify-content-between align-items-start mb-3">
            <div class="skeleton-text-short"></div>
            <div class="skeleton-badge"></div>
          </div>

          <div class="task-info mb-3">
            <div class="skeleton-text-line"></div>
            <div class="skeleton-text-line"></div>
          </div>

          <div class="d-flex justify-content-end mt-2">
            <div class="skeleton-button"></div>
          </div>
        </div>
      </div>

      <div class="card task-card">
        <div class="card-body">
          <div class="d-flex justify-content-between align-items-start mb-3">
            <div class="skeleton-text-short"></div>
            <div class="skeleton-badge"></div>
          </div>

          <div class="task-info mb-3">
            <div class="skeleton-text-line"></div>
            <div class="skeleton-text-line"></div>
          </div>

          <div class="d-flex justify-content-end mt-2">
            <div class="skeleton-button"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- 底部导航栏 -->
<div class="nav-bottom">
  <a href="/h5/index" class="nav-item active">
    <div class="nav-icon">
      <i class="bi bi-house-door-fill"></i>
    </div>
    <div>首页</div>
  </a>
  <a href="/h5/my" class="nav-item">
    <div class="nav-icon">
      <i class="bi bi-person"></i>
    </div>
    <div>我的</div>
  </a>
</div>

<!-- jQuery -->
<script th:src="@{/static/js/jquery-3.6.0.min.js}"></script>
<!-- Bootstrap JS -->
<script th:src="@{/static/js/bootstrap.bundle.min.js}"></script>

<style>
  /* 骨架屏样式 */
  @keyframes shimmer {
    0% {
      background-position: -1000px 0;
    }
    100% {
      background-position: 1000px 0;
    }
  }

  .skeleton-loader {
    display: block;
    width: 100%;
  }

  .skeleton-text-short, .skeleton-text-line, .skeleton-badge, .skeleton-button {
    background: linear-gradient(to right, #f0f0f0 8%, #e0e0e0 18%, #f0f0f0 33%);
    background-size: 1000px 100%;
    animation: shimmer 2s infinite linear;
    border-radius: 4px;
  }

  .skeleton-text-short {
    height: 18px;
    width: 50%;
    margin-bottom: 10px;
  }

  .skeleton-text-line {
    height: 14px;
    width: 80%;
    margin-bottom: 10px;
  }

  .skeleton-badge {
    height: 22px;
    width: 60px;
    border-radius: 20px;
  }

  .skeleton-button {
    height: 38px;
    width: 100px;
    border-radius: 12px;
  }

</style>

<script th:inline="javascript">
    $(document).ready(function() {
      // 立即开始加载数据
      loadTasks();

      function loadTasks() {
        // 获取当前周期ID
        const periodId = /*[[${currentPeriod != null ? currentPeriod.id : 0}]]*/'0';

        if (periodId == 0) {
          showEmptyState('暂无测评任务', '当前无进行中的评测周期');
          return;
        }

        // 设置请求超时
        let timeoutId = setTimeout(function() {
          showError('加载超时，请检查网络连接后重试');
        }, 10000); // 10秒超时

        // 从后端获取任务列表
        $.ajax({
          url: '/api/assessment/tasks?periodId=' + periodId,
          method: 'GET',
          headers: {
            'Authorization': 'Bearer ' + localStorage.getItem('token')
          },
          success: function(resp) {
            // 清除超时计时器
            clearTimeout(timeoutId);

            if (resp.code === 200) {
              renderTasks(resp.data);
            } else {
              showError(resp.message);
            }
          },
          error: function(xhr, status, error) {
            // 清除超时计时器
            clearTimeout(timeoutId);

            // 显示具体错误信息
            if (status === 'timeout') {
              showError('请求超时，请检查网络连接后重试');
            } else if (xhr.status === 0) {
              showError('网络连接中断，请检查您的网络连接');
            } else if (xhr.status === 401) {
              showError('登录已过期，请重新登录');
              setTimeout(function() {
                window.location.href = '/h5/login';
              }, 1500);
            } else {
              showError('获取任务列表失败: ' + (error || '未知错误'));
            }
          },
          timeout: 8000 // 8秒超时设置
        });
      }

      function renderTasks(tasks) {
        if (!tasks || tasks.length === 0) {
          showEmptyState('暂无测评任务', '当前周期内没有分配给您的测评任务');
          return;
        }

        let html = '';
        tasks.forEach(function(task, index) {
          const isCompleted = task.assessStatus === 1;

          html += `
            <div class="card task-card" style="animation-delay: ${0.4 + (index * 0.1)}s">
              <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-3">
                  <h6 class="card-title mb-0 fw-bold">${task.assesseeName}</h6>
                  <span class="badge ${isCompleted ? 'task-badge-completed' : 'task-badge-pending'} rounded-pill">
                    ${isCompleted ? '已完成' : '待评测'}
                  </span>
                </div>

                <div class="task-info mb-3">
                  <div class="task-meta">
                    <i class="bi bi-building"></i>
                    <span>${task.assesseeDepartment || '未分配部门'}</span>
                  </div>
                  <div class="task-meta">
                    <i class="bi bi-briefcase"></i>
                    <span>${task.assesseePosition || '未分配职位'}</span>
                  </div>
                </div>

                <div class="d-flex justify-content-end mt-2">
                  <a href="/h5/task/detail?relationId=${task.relationId}"
                     class="btn ${isCompleted ? 'btn-outline-primary' : 'btn-primary'} btn-action">
                    <i class="bi ${isCompleted ? 'bi-eye' : 'bi-pencil-square'} me-1"></i>
                    ${isCompleted ? '查看详情' : '去评测'}
                  </a>
                </div>
              </div>
            </div>
          `;
        });

        $('#taskList').html(html);
        // 添加动画类
        setTimeout(function() {
          $('.task-card').addClass('fadeInUp');
        }, 100);
      }

      function showEmptyState(message, description) {
        $('#taskList').html(`
          <div class="empty-state">
            <i class="bi bi-inbox empty-state-icon"></i>
            <p class="text-muted mb-2">${message}</p>
            <small class="text-muted">${description}</small>
          </div>
        `);
      }

      function showError(message) {
        $('#taskList').html(`
          <div class="alert alert-danger rounded-3 d-flex align-items-center p-4 shadow-sm" role="alert">
            <i class="bi bi-exclamation-triangle-fill me-3 fs-4 text-danger"></i>
            <div>${message}</div>
          </div>
          <div class="text-center mt-4">
            <button class="btn btn-outline-primary btn-sm rounded-pill px-4 py-2" onclick="loadTasks()">
              <i class="bi bi-arrow-repeat me-1"></i>重试
            </button>
          </div>
        `);
      }
    });

</script>
</body>
</html>
